package fr.imag.ufrima.ctte.gui.toolbars;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JToolBar;

import fr.imag.ufrima.ctte.ImagCTTE;
import fr.imag.ufrima.ctte.TaskDiagram;
import fr.imag.ufrima.ctte.events.ImagCtteAdapter;
import fr.imag.ufrima.ctte.events.ImagCtteEvent;
import fr.imag.ufrima.ctte.events.ImagCtteListener;
import fr.imag.ufrima.ctte.events.TaskDiagramAdapter;
import fr.imag.ufrima.ctte.events.TaskDiagramEvent;
import fr.imag.ufrima.ctte.gui.dialogs.FindDialog;

public class MainToolBar extends JToolBar {
	private ImagCTTE imagCTTE;
	/**
	 * Constructor
	 */
	public MainToolBar(ImagCTTE imagCTTE){
		this.imagCTTE = imagCTTE;
		InitComponents();
		imagCTTE.addImagCtteListener(new ImagCtteAdapter(){			
			public void taskDiagramSwitched(ImagCtteEvent event) {
				super.taskDiagramSwitched(event);
				TaskDiagram taskDiagram = event.getTaskDiagram();
				if (taskDiagram == null){
					tlbSaveButton.setEnabled(false);
					tlbSaveAsImageButton.setEnabled(false);
					tlbPrintButton.setEnabled(false);
					
					tlbFind.setEnabled(false);
					
				} else {
					tlbSaveButton.setEnabled(true);
					tlbSaveAsImageButton.setEnabled(true);
					tlbPrintButton.setEnabled(true);
					tlbFind.setEnabled(true);
				}
				taskDiagram.addTaskDiagramListener(new TaskDiagramAdapter(){					
					public void selectedOperatorChanged(TaskDiagramEvent event) {
						if (event.getOperator() != null){
							tlbDeleteButton.setEnabled(true);
						} /*else {
							tlbDeleteButton.setEnabled(false);
						}*/
					}					
					public void selectedTaskChanged(TaskDiagramEvent event) {
						if (event.getTask() != null && event.getTask().getTopTask() != null){							
							tlbDeleteButton.setEnabled(true);
						} else {
							tlbDeleteButton.setEnabled(false);

						}
					}
				});
			}
		});
		
	}
	/**
	 * Initialize components
	 */
	protected void InitComponents(){
		// main toolbar buttons
        //mainToolBar = new javax.swing.JToolBar();        
        tlbNewButton = new javax.swing.JButton();
        tlbOpenButton = new javax.swing.JButton();        
        tlbSaveButton = new javax.swing.JButton();
        tlbSaveAsImageButton = new javax.swing.JButton();
        tlbPrintButton = new javax.swing.JButton();        
        tlbCutButton = new javax.swing.JButton();
        tlbCutSubTreeButton = new javax.swing.JButton();
        tlbCopyButton = new javax.swing.JButton();
        tlbPastButton = new javax.swing.JButton();
        tlbDeleteButton = new javax.swing.JButton();                        
        tlbUndoButton = new javax.swing.JButton();
        tlbRedoButton = new javax.swing.JButton();
        tlbFind = new javax.swing.JButton();
        
     // main toolbar        
        tlbNewButton.setIcon(new javax.swing.ImageIcon("images\\new.gif"));
        tlbNewButton.setEnabled(true);
        tlbNewButton.setFocusable(false);
        tlbNewButton.setToolTipText("New");
        tlbNewButton.addActionListener(new ActionListener(){        	
        	public void actionPerformed(ActionEvent arg0) {
        		if (imagCTTE != null) imagCTTE.newTaskDiagram();
        	}
        });
        this.add(tlbNewButton);
        tlbOpenButton.setIcon(new javax.swing.ImageIcon("images\\open.gif"));
        tlbNewButton.setEnabled(true);
        tlbOpenButton.setFocusable(false);
        tlbOpenButton.setToolTipText("Open");
        this.add(tlbOpenButton);
        tlbSaveButton.setIcon(new javax.swing.ImageIcon("images\\save.gif"));
        tlbSaveButton.setEnabled(false);
        tlbSaveButton.setFocusable(false);
        tlbSaveButton.setToolTipText("Save");
        this.add(tlbSaveButton);
        tlbSaveAsImageButton.setIcon(new javax.swing.ImageIcon("images\\savegif.gif"));
        tlbSaveAsImageButton.setEnabled(false);
        tlbSaveAsImageButton.setFocusable(false);
        tlbSaveAsImageButton.setToolTipText("Save as image");
        this.add(tlbSaveAsImageButton);
        tlbPrintButton.setIcon(new javax.swing.ImageIcon("images\\print.gif"));
        tlbPrintButton.setEnabled(false);
        tlbPrintButton.setFocusable(false);
        tlbPrintButton.setToolTipText("Print");
        this.add(tlbPrintButton);        

        this.add(new javax.swing.JToolBar.Separator());
        
        tlbCutButton.setIcon(new javax.swing.ImageIcon("images\\cut.gif"));
        tlbCutButton.setEnabled(false);
        tlbCutButton.setFocusable(false);
        tlbCutButton.setToolTipText("Cut");
        this.add(tlbCutButton);
        tlbCutSubTreeButton.setIcon(new javax.swing.ImageIcon("images\\cutall.gif"));
        tlbCutSubTreeButton.setEnabled(false);
        tlbCutSubTreeButton.setFocusable(false);
        tlbCutSubTreeButton.setToolTipText("Cut SubTree");
        this.add(tlbCutSubTreeButton);
        tlbCopyButton.setIcon(new javax.swing.ImageIcon("images\\copy.gif"));
        tlbCopyButton.setEnabled(false);
        tlbCopyButton.setFocusable(false);
        tlbCopyButton.setToolTipText("Copy");
        this.add(tlbCopyButton);
        tlbPastButton.setIcon(new javax.swing.ImageIcon("images\\paste.gif"));
        tlbPastButton.setEnabled(false);
        tlbPastButton.setFocusable(false);
        tlbPastButton.setToolTipText("Past");
        this.add(tlbPastButton);        
        tlbDeleteButton.setIcon(new javax.swing.ImageIcon("images\\remove.png"));
        tlbDeleteButton.setEnabled(false);
        tlbDeleteButton.setFocusable(false);
        tlbDeleteButton.setToolTipText("Delete");
        tlbDeleteButton.addActionListener(new ActionListener(){        	
        	public void actionPerformed(ActionEvent arg0) {
        		if (imagCTTE != null && imagCTTE.getCurrentTaskDiagram() != null){
        			if (imagCTTE.getCurrentTaskDiagram().getSelectedOperator() != null)        			
        				imagCTTE.getCurrentTaskDiagram().removeOperator();
        			else if (imagCTTE.getCurrentTaskDiagram().getSelectedTask() != null)
        			imagCTTE.getCurrentTaskDiagram().removeTask();
        			
        		}        		
        	}
        });
        this.add(tlbDeleteButton);

        this.add(new javax.swing.JToolBar.Separator());        
        
        tlbUndoButton.setIcon(new javax.swing.ImageIcon("images\\undo.gif"));
        tlbUndoButton.setEnabled(false);
        tlbUndoButton.setFocusable(false);
        tlbUndoButton.setToolTipText("Undo");
        this.add(tlbUndoButton);
        tlbRedoButton.setIcon(new javax.swing.ImageIcon("images\\redo.gif"));
        tlbRedoButton.setEnabled(false);
        tlbRedoButton.setFocusable(false);
        tlbRedoButton.setToolTipText("Redo");
        this.add(tlbRedoButton);
        
        this.add(new javax.swing.JToolBar.Separator());
        
        tlbFind.setIcon(new javax.swing.ImageIcon("images\\taskfind.png"));
        tlbFind.setEnabled(false);
        tlbFind.setFocusable(false);
        tlbFind.setToolTipText("Find a Task");
        tlbFind.addActionListener(new ActionListener(){        	
        	public void actionPerformed(ActionEvent arg0) {
        		FindDialog findDialog = new FindDialog(imagCTTE);
        		findDialog.setModal(true);
        		findDialog.setVisible(true);
        	}
        });
        this.add(tlbFind);
	}
	/**
	 * Components
	 */
	//private javax.swing.JToolBar mainToolBar;
    private javax.swing.JButton tlbNewButton;
    private javax.swing.JButton tlbOpenButton;
    private javax.swing.JButton tlbSaveButton;
    private javax.swing.JButton tlbSaveAsImageButton;
    private javax.swing.JButton tlbPrintButton;    
    private javax.swing.JButton tlbCutButton;
    private javax.swing.JButton tlbCutSubTreeButton;    
    private javax.swing.JButton tlbCopyButton;
    private javax.swing.JButton tlbPastButton;
    private javax.swing.JButton tlbDeleteButton;
    private javax.swing.JButton tlbUndoButton;
    private javax.swing.JButton tlbRedoButton;
    private javax.swing.JButton tlbFind;
}
